package com.example.demo.cmiemail;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Hy on 2018/8/6.
 */
public class ExecNewThreadFunctionHelper {
    private static Log log =LogFactory.getLog(ExecNewThreadFunctionHelper.class);
    //创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待。
    private static final ExecutorService executorService = Executors.newFixedThreadPool(3000);
    /**
     * 多线程发送邮件
     * @param htmlEmail
     */
    public static void sendEmail(final HtmlEmail htmlEmail) {
     executorService.execute(new Runnable() {
         @Override
         public void run() {
             try {
                 String result = htmlEmail.send();
                 log.info("sendEmail the result:"+result);
             } catch (EmailException e) {
                log.error("=============>调用邮件微服务时发生错误",e);
             }
         }
     });
    }

}
